package ak;

import javax.xml.bind.DatatypeConverter;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/9/29 0:11
 */
public class SapTest {


    public static String getMD5(String src) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] digest  = md5.digest(src.getBytes(StandardCharsets.UTF_8));
        return new BigInteger(1, digest).toString(16);
    }

    private static String getSignature(String accessKey, String secretKey, String timestamp) {
        return md5(timestamp + accessKey + secretKey);
    }

    public static String md5(Object str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(String.valueOf(str).getBytes());
            byte[] digest = md.digest();
            return DatatypeConverter.printHexBinary(digest);
        } catch (NoSuchAlgorithmException e) {

        }
        return null;
    }

    public static void main(String[] args) {
        //sit
//        String ak = "Ha3V73Lphqudvmfn";
//        String sk = "gkreRneBNJRznmjvzS2QB42nJ9jlG8";

        //dev
        String ak = "fmFJreW2n30rO3pj";
        String sk = "v0hRFa1K1F0YDOES4w6ifPPtQLEmzZ";

        //me dev
        //String ak = "DNGruiErQo9kqpxH";
        //String sk = "D0xJXN3q8AYnE9cnFPXIxtwhT2Lhh5";


        String times = String.valueOf(System.currentTimeMillis());//String.valueOf(System.currentTimeMillis());//"1634386507159";
        String signature = getSignature(ak, sk, times);
        System.out.println("timestamp=="+times + ",token==" + signature);

        //时间戳+ak+sk+MD5加密
        String src =  times+ak+sk;
        System.out.println("timestamp=="+times + ",token==" + getMD5(src));

        String messageId = UUID.randomUUID().toString();
        System.out.println("messageId====" + messageId.replaceAll("-", ""));
    }

}

